<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitepage
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: Search.php 2011-05-05 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitepage_Form_Search extends Fields_Form_Search {
	
	protected $_searchForm;
	
	public function init() {
		// Add custom elements
		$this->setAttribs ( array ('id' => 'filter_form', 'class' => 'global_form_box' ) )->setAction ( Zend_Controller_Front::getInstance ()->getRouter ()->assemble ( array () ) )->setMethod ( 'GET' );
		$this->_searchForm = Engine_Api::_ ()->getDbTable ( 'searchform', 'sitepage' );
		//if (Engine_Api::_()->getApi('settings', 'core')->getSetting('sitepage.profile.search', 1)) {
		$this->getMemberTypeElement ();
		//}
		

		//$this->getDisplayNameElement();
		$this->getAdditionalOptionsElement ();
		
		parent::init ();
		
		$this->loadDefaultDecorators ();
		$view = Zend_Registry::isRegistered ( 'Zend_View' ) ? Zend_Registry::get ( 'Zend_View' ) : null;
		$this->setAction ( $view->url ( array ('action' => 'index' ), 'sitepage_general', true ) )->getDecorator ( 'HtmlTag' )->setOption ( 'class', 'browsemembers_criteria' );
	}
	
	public function getMemberTypeElement() {
		$row = $this->_searchForm->getFieldsOptions ( 'profile_type' );
		if (empty ( $row ) || empty ( $row->display )) {
			return;
		}
		$multiOptions = array ('' => ' ' );
		$profileTypeFields = Engine_Api::_ ()->fields ()->getFieldsObjectsByAlias ( $this->_fieldType, 'profile_type' );
		if (count ( $profileTypeFields ) !== 1 || ! isset ( $profileTypeFields ['profile_type'] ))
			return;
		$profileTypeField = $profileTypeFields ['profile_type'];
		
		$options = $profileTypeField->getOptions ();
		
		if (count ( $options ) <= 1) {
			if (count ( $options ) == 1) {
				$this->_topLevelId = $profileTypeField->field_id;
				$this->_topLevelValue = $options [0]->option_id;
			}
			return;
		}
		
		foreach ( $options as $option ) {
			$multiOptions [$option->option_id] = $option->label;
		}
		
		asort ( $multiOptions );
		
		$this->addElement ( 'Select', 'profile_type', array ('label' => 'Page Profile Type', // 'order' => 99,
		'order' => $row->order, 'class' => 'field_toggle' . ' ' . 'parent_' . 0 . ' ' . 'option_' . 0 . ' ' . 'field_' . $profileTypeField->field_id . ' ', 'onchange' => 'changeFields($(this));', 'decorators' => array ('ViewHelper', array ('Label', array ('tag' => 'span' ) ), array ('HtmlTag', array ('tag' => 'li' ) ) ), 'multiOptions' => $multiOptions ) );
		return $this->profile_type;
	}
	
	public function getDisplayNameElement() {
		$this->addElement ( 'Text', 'displayname', array ('label' => 'Name', 'order' => - 1000000, 'decorators' => array ('ViewHelper', array ('Label', array ('tag' => 'span' ) ), array ('HtmlTag', array ('tag' => 'li' ) ) ) )//'onkeypress' => 'return submitEnter(event)',
		 );
		return $this->displayname;
	}
	
	public function getAdditionalOptionsElement() {
		$subform = new Zend_Form_SubForm ( array ('name' => 'extra', 'order' => 19999999, 'decorators' => array ('FormElements' ) ) );
		Engine_Form::enableForm ( $subform );
		
		//   public function getAdditionalOptionsElement() {
		$i = - 5000;
		
		$this->addElement ( 'Hidden', 'page', array ('order' => $i -- ) );
		
		$this->addElement ( 'Hidden', 'tag', array ('order' => $i -- ) );
		
		$this->addElement ( 'Hidden', 'start_date', array ('order' => $i -- ) );
		
		$this->addElement ( 'Hidden', 'end_date', array ('order' => $i -- ) );
		$row = $this->_searchForm->getFieldsOptions ( 'price' );
		if (! empty ( $row ) && ! empty ( $row->display )) {
			
			$searchForm = Engine_Api::_ ()->getDbTable ( 'searchform', 'sitepage' );
			$enablePrice = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.price.field', 1 );
			if (! empty ( $enablePrice )) {
				
				$subformPrice = new Zend_Form_SubForm ( array ('description' => "Price", // 'order' => $i--,
				'order' => $row->order, 'decorators' => array ('FormElements', array ('Description', array ('placement' => 'PREPEND', 'tag' => 'span' ) ), array ('HtmlTag', array ('tag' => 'li', 'class' => 'browse-range-wrapper' ) ) ) ) );
				Fields_Form_Standard::enableForm ( $subformPrice );
				Engine_Form::enableForm ( $subformPrice );
				
				$params ['options'] ['decorators'] = array ('ViewHelper' );
				$subformPrice->addElement ( 'text', 'min', $params ['options'] );
				$subformPrice->addElement ( 'text', 'max', $params ['options'] );
				$this->addSubForm ( $subformPrice, 'sitepage_price' );
			}
		}
		
		$rowLocation = $this->_searchForm->getFieldsOptions ( 'location' );
		if (! empty ( $rowLocation ) && ! empty ( $rowLocation->display )) {
			$enableLocation = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.locationfield', 1 );
			if (! empty ( $enableLocation )) {
				$row = $this->_searchForm->getFieldsOptions ( 'locationmiles' );
				if (! empty ( $row ) && ! empty ( $row->display )) {
					$enableProximitysearch = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.proximitysearch', 1 );
					
					if (! empty ( $enableProximitysearch )) {
						$flage = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.proximity.search.kilometer', 0 );
						if ($flage) {
							$locationLable = "Within Kilometers";
							$locationOption = array ('0' => '', '1' => '1 Kilometer', '2' => '2 Kilometers', '5' => '5 Kilometers', '10' => '10 Kilometers', '20' => '20 Kilometers', '50' => '50 Kilometers', '100' => '100 Kilometers', '250' => '250 Kilometers', '500' => '500 Kilometers', '750' => '750 Kilometers', '1000' => '1000 Kilometers' );
						} else {
							$locationLable = "Within Miles";
							$locationOption = array ('0' => '', '1' => '1 Mile', '2' => '2 Miles', '5' => '5 Miles', '10' => '10 Miles', '20' => '20 Miles', '50' => '50 Miles', '100' => '100 Miles', '250' => '250 Miles', '500' => '500 Miles', '750' => '750 Miles', '1000' => '1000 Miles' );
						}
						$this->addElement ( 'Select', 'locationmiles', array ('label' => $locationLable, 'multiOptions' => $locationOption, 'value' => '0', 'order' => $row->order, 'decorators' => array ('ViewHelper', array ('Label', array ('tag' => 'span' ) ), array ('HtmlTag', array ('tag' => 'li' ) ) ) ) );
					}
				}
				$this->addElement ( 'Text', 'sitepage_location', array ('label' => 'Location', 'order' => $rowLocation->order, 'decorators' => array ('ViewHelper', array ('Label', array ('tag' => 'span' ) ), array ('HtmlTag', array ('tag' => 'li' ) ) ) ) );
			}
		}
		$row = $this->_searchForm->getFieldsOptions ( 'search' );
		if (! empty ( $row ) && ! empty ( $row->display )) {
			$this->addElement ( 'Text', 'search', array ('label' => 'Search Pages', 'order' => $row->order, 'decorators' => array ('ViewHelper', array ('Label', array ('tag' => 'span' ) ), array ('HtmlTag', array ('tag' => 'li' ) ) ) ) );
		}
		
		$row = $this->_searchForm->getFieldsOptions ( 'badge_id' );
		if (! empty ( $row ) && ! empty ( $row->display )) {
			if (( int ) Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagebadge' ) && Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepagebadge.seaching.bybadge', 1 )) {
				
				$params = array ();
				$params ['search_code'] = 1;
				$badgeData = Engine_Api::_ ()->getDbTable ( 'badges', 'sitepagebadge' )->getBadgesData ( $params );
				if (! empty ( $badgeData )) {
					$badgeData = $badgeData->toArray ();
					$badgeCount = Count ( $badgeData );
					
					if (! empty ( $badgeCount )) {
						$badge_options = array ();
						$badge_options [0] = '';
						foreach ( $badgeData as $key => $name ) {
							$badge_options [$name ['badge_id']] = $name ['title'];
						}
						
						$this->addElement ( 'Select', 'badge_id', array ('label' => 'Badge', 'multiOptions' => $badge_options, 'onchange' => 'searchSitepages();', 'order' => $row->order, 'decorators' => array ('ViewHelper', array ('Label', array ('tag' => 'span' ) ), array ('HtmlTag', array ('tag' => 'li' ) ) ) ) );
					}
				}
			}
		}
		$row = $this->_searchForm->getFieldsOptions ( 'orderby' );
		$sitepagereviewEnabled = Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagereview' );
		if (! empty ( $row ) && ! empty ( $row->display ) && ! empty ( $sitepagereviewEnabled )) {
			$this->addElement ( 'Select', 'orderby', array ('label' => 'Browse By', 'multiOptions' => array ('' => '', 'creation_date' => 'Most Recent', 'view_count' => 'Most Viewed', 'comment_count' => 'Most Commented', 'like_count' => 'Most Liked', 'title' => "Alphabetical", 'review_count' => "Most Reviewed", 'rating' => "Highest Rated" ), 'onchange' => 'searchSitepages();', 'order' => $row->order, 'decorators' => array ('ViewHelper', array ('Label', array ('tag' => 'span' ) ), array ('HtmlTag', array ('tag' => 'li' ) ) ) ) );
		} elseif (! empty ( $row ) && ! empty ( $row->display )) {
			$this->addElement ( 'Select', 'orderby', array ('label' => 'Browse By', 'multiOptions' => array ('' => '', 'creation_date' => 'Most Recent', 'view_count' => 'Most Viewed', 'comment_count' => 'Most Commented', 'like_count' => 'Most Liked', 'title' => "Alphabetical" ), 'onchange' => 'searchSitepages();', 'order' => $row->order, 'decorators' => array ('ViewHelper', array ('Label', array ('tag' => 'span' ) ), array ('HtmlTag', array ('tag' => 'li' ) ) ) ) );
		} else {
			$this->addElement ( 'hidden', 'orderby', array () );
		}
		$row = $this->_searchForm->getFieldsOptions ( 'show' );
		if (! empty ( $row ) && ! empty ( $row->display )) {
			$show_multiOptions = array ();
			$show_multiOptions ["1"] = 'Everyone\'s Pages';
			$show_multiOptions ["2"] = 'Only My Friends\' Pages';
			$value_deault = 1;
			$enableNetwork = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.network', 0 );
			if (empty ( $enableNetwork )) {
				$viewer_id = Engine_Api::_ ()->user ()->getViewer ()->getIdentity ();
				$networkMembershipTable = Engine_Api::_ ()->getDbtable ( 'membership', 'network' );
				$viewerNetwork = $networkMembershipTable->fetchRow ( array ('user_id = ?' => $viewer_id ) );
				
				if (! empty ( $viewerNetwork ) || Engine_Api::_ ()->getApi ( 'subCore', 'sitepage' )->pageBaseNetworkEnable ()) {
					$show_multiOptions ["3"] = 'Only My Networks';
					$browseDefaulNetwork = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.default.show', 0 );
					
					if (! isset ( $_GET ['show'] ) && ! empty ( $browseDefaulNetwork )) {
						$value_deault = 3;
					} elseif (isset ( $_GET ['show'] )) {
						$value_deault = $_GET ['show'];
					}
				}
			}
			
			$this->addElement ( 'Select', 'show', array ('label' => 'Show', 'multiOptions' => $show_multiOptions, 'onchange' => 'searchSitepages();', 'order' => $row->order, 'decorators' => array ('ViewHelper', array ('Label', array ('tag' => 'span' ) ), array ('HtmlTag', array ('tag' => 'li' ) ) ), 'value' => $value_deault ) );
		} else {
			$this->addElement ( 'hidden', 'show', array ('value' => 1 ) );
		}
		$row = $this->_searchForm->getFieldsOptions ( 'closed' );
		if (! empty ( $row ) && ! empty ( $row->display )) {
			$enableStatus = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.status.show', 1 );
			if ($enableStatus) {
				$this->addElement ( 'Select', 'closed', array ('label' => 'Status', 'multiOptions' => array ('' => 'All Pages', '0' => 'Only Open Pages', '1' => 'Only Closed Pages' ), 'onchange' => 'searchSitepages();', 'order' => $row->order, 'decorators' => array ('ViewHelper', array ('Label', array ('tag' => 'span' ) ), array ('HtmlTag', array ('tag' => 'li' ) ) ) ) );
			}
		}
		
		$row = $this->_searchForm->getFieldsOptions ( 'category_id' );
		if (! empty ( $row ) && ! empty ( $row->display )) {
			// prepare categories
			$categories = Engine_Api::_ ()->getDbTable ( 'categories', 'sitepage' )->getCategories ();
			if (count ( $categories ) != 0) {
				$categories_prepared [0] = "";
				foreach ( $categories as $category ) {
					$categories_prepared [$category->category_id] = $category->category_name;
				}
				
				// category field
				$this->addElement ( 'Select', 'category_id', array ('label' => 'Category', 'order' => $row->order, 'multiOptions' => $categories_prepared, 'onchange' => "subcategoryies(this.value, '', '', '');", 'decorators' => array ('ViewHelper', array ('Label', array ('tag' => 'span' ) ), array ('HtmlTag', array ('tag' => 'li' ) ) ) ) );
			}
			
			$this->addElement ( 'Select', 'subcategory_id', array ('RegisterInArrayValidator' => false, 'order' => $row->order + 1, 'decorators' => array (array ('ViewScript', array ('viewScript' => 'application/modules/Sitepage/views/scripts/_Subcategory.tpl', 'class' => 'form element' ) ) ) ) );
			
			$this->addElement ( 'Select', 'subsubcategory_id', array ('RegisterInArrayValidator' => false, 'order' => $row->order + 1, 'decorators' => array (array ('ViewScript', array ('viewScript' => 'application/modules/Sitepage/views/scripts/_Subcategory.tpl', 'class' => 'form element' ) ) ) ) );
		
		} else {
			$this->addElement ( 'Hidden', 'category_id', array ('order' => $i -- ) );
			
			$this->addElement ( 'Hidden', 'subcategory_id', array ('order' => $i -- ) );
			
			$this->addElement ( 'Hidden', 'subsubcategory_id', array ('order' => $i -- ) );
		}
		
		$this->addElement ( 'Hidden', 'category', array ('order' => $i -- ) );
		
		$this->addElement ( 'Hidden', 'subcategory', array ('order' => $i -- ) );
		
		$this->addElement ( 'Hidden', 'subsubcategory', array ('order' => $i -- ) );
		
		$this->addElement ( 'Hidden', 'categoryname', array ('order' => $i -- ) );
		
		$this->addElement ( 'Hidden', 'subcategoryname', array ('order' => $i -- ) );
		
		$this->addElement ( 'Hidden', 'subsubcategoryname', array ('order' => $i -- ) );
		
		$row = $this->_searchForm->getFieldsOptions ( 'has_photo' );
		if (! empty ( $row ) && ! empty ( $row->display )) {
			$this->addElement ( 'Checkbox', 'has_photo', array ('label' => 'Only Pages With Photos', 'order' => $row->order, 'decorators' => array ('ViewHelper', array ('Label', array ('placement' => 'APPEND', 'tag' => 'label' ) ), array ('HtmlTag', array ('tag' => 'li' ) ) ) ) );
		}
		
		$row = $this->_searchForm->getFieldsOptions ( 'has_review' );
		if (! empty ( $row ) && ! empty ( $row->display ) && ( int ) Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagereview' )) {
			$this->addElement ( 'Checkbox', 'has_review', array ('label' => 'Only Pages With Reviews', 'order' => $row->order, 'decorators' => array ('ViewHelper', array ('Label', array ('placement' => 'APPEND', 'tag' => 'label' ) ), array ('HtmlTag', array ('tag' => 'li' ) ) ) ) );
		}
		
		if (( int ) Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagegeolocation' )) {
			$row = $this->_searchForm->getFieldsOptions ( 'has_currentlocation' );
			if (! empty ( $row ) && ! empty ( $row->display )) {
				$this->addElement ( 'Checkbox', 'has_currentlocation', array ('label' => 'Only current place and range', 'order' => $row->order, 'onchange' => 'searchSitepages();', 'decorators' => array ('ViewHelper', array ('Label', array ('placement' => 'APPEND', 'tag' => 'label' ) ), array ('HtmlTag', array ('tag' => 'li' ) ) ), 'value' => Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sgl.geolocation.default', 1 ) ) );
			}
		}
		$subform->addElement ( 'Button', 'done', array ('label' => 'Search', 'type' => 'submit', 'ignore' => true ) );
		
		$this->addSubForm ( $subform, $subform->getName () );
		
		return $this;
	}

}
?>